﻿Imports Stardust.Core
Imports Stardust.Core.Controls

Partial Public Class BookmarkManager

    Public bSections As New Dictionary(Of String, String) 'Bookmark Sections
    Dim CurSection As New List(Of ImageObject) 'Current Bookmark Section
    Dim CurTitle As String

    Public Event ListUpdated(ByVal List As Dictionary(Of String, String))

    Private Sub UpdateSection(ByVal Items As List(Of ImageObject))
        Dim data = <?xml version="1.0" encoding="utf-8"?>
                   <ImageList Title=<%= CurTitle %>>
                       <%= From item As ImageObject In Items _
                           Select <Image><%= item.OriginalUri %></Image> %>
                   </ImageList>
        Dim file As String = bSections(ListBox1.SelectedItem.ToString)
        Dim sw As New IO.StreamWriter(file)
        sw.Write(data.ToString)
        sw.Close()


    End Sub

    Private Sub BookmarkManager_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        e.Cancel = True
        Me.ListBox1.Items.Clear()
        Me.ListBox2.Items.Clear()
        'bSections.Clear()
        CurSection.Clear()
        CurTitle = ""
        Me.Visibility = Windows.Visibility.Hidden
    End Sub

    Private Sub mnu_RemoveImage_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        CurSection.Remove(ListBox2.SelectedItem.Base)
        UpdateSection(CurSection)
        ListBox2.Items.Remove(ListBox2.SelectedItem)

    End Sub

    Private Sub mnu_CreateSection_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            Dim dlg As New InputDialog
            If dlg.ShowDialog("Create bookmark section", _
                              "Write the title of the new bookmark section") = Forms.DialogResult.OK Then
                Dim fn As String = String.Format("{0}\{1}\{2}.sil", StardustDir, "Bookmarks", dlg.InputText)
                Dim fi As New IO.FileInfo(fn)
                Dim [Continue] As Boolean = False
                If fi.Exists = True Then
                    Select Case MsgBox("If you continue creating this bookmark section another one will be lost." & _
                                       vbCrLf & "Do you want no continue creating this section?", MsgBoxStyle.YesNo)
                        Case MsgBoxResult.Yes
                            [Continue] = True
                        Case Else
                            [Continue] = False
                    End Select
                Else
                    [Continue] = True
                End If
                If [Continue] = True Then
                    Dim data = <?xml version="1.0" encoding="utf-8"?>
                               <ImageList Title=<%= dlg.InputText %>>

                               </ImageList>
                    Dim sw As New IO.StreamWriter(fn)
                    sw.Write(data.ToString)
                    sw.Close()
                    bSections.Add(dlg.InputText, fn)
                    RaiseEvent ListUpdated(bSections)
                    ListBox1.Items.Add(dlg.InputText)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub mnu_RemoveSection_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        'Try
        ListBox2.Items.Clear()
        Dim fn As String = bSections(ListBox1.SelectedItem.ToString)
        Dim fi As New IO.FileInfo(fn)
        fi.Delete()
        bSections.Remove(ListBox1.SelectedItem.ToString)
        ListBox1.Items.Remove(ListBox1.SelectedItem)
        RaiseEvent ListUpdated(bSections)
        'Catch ex As Exception
        'MsgBox(ex.Message)
        'End Try
    End Sub

    Private Sub ListBox1_SelectionChanged_1(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ListBox1.SelectionChanged
        Try
            If ListBox1.Items.Count > 0 Then
                ListBox2.Items.Clear()
                CurSection.Clear()
                Console.WriteLine(ListBox1.SelectedItem.ToString)
                Dim file As String = bSections(ListBox1.SelectedItem.ToString)
                Dim xDoc As XDocument = XDocument.Load(file)

                CurTitle = xDoc.<ImageList>.@Title

                Dim data = From _image In xDoc...<Image> _
                           Select _image, _image.@TypeOf

                For Each item In data
                    Dim _i As New ImageObject(New Uri(item._image.Value))
                    CurSection.Add(_i)

                    ListBox2.Items.Add(New ImageControl(_i))

                Next
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

    End Sub

    Public Overloads Sub Show()
        bSections.Clear()
        Dim sd As New IO.DirectoryInfo(StardustDir & "\bookmarks")
        If sd.Exists = False Then
            sd.Create()
        End If
        For Each file As IO.FileInfo In sd.GetFiles("*.sil") 'Stardust image List
            Try
                Dim xDoc As XDocument = XDocument.Load(file.FullName)
                Dim title As String = xDoc.<ImageList>.@Title
                bSections.Add(title, file.FullName)

                Dim _images = From item In xDoc...<ImageList>.<Image> Select New Uri(item.Value) Take 3

                Dim AlbumPreview As New Core.Controls.AlbumPreview With {.AlbumTitle = title,
                                                                         .AlbumBase = title,
                                                                         .Width = 175, .Height = 55}
                Try
                    AlbumPreview.FillPreview(_images.ToList)
                Catch ex As Exception
                    Console.WriteLine(ex.Message)
                End Try
                ListBox1.Items.Add(AlbumPreview)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        Next
        Me.Visibility = Windows.Visibility.Visible
    End Sub

    Public Sub AddSection_Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            Dim dlg As New InputDialog

            If dlg.ShowDialog("Create bookmark section", _
                              "Write the title of the new bookmark section") = Forms.DialogResult.OK Then
                Dim fn As String = String.Format("{0}\{1}\{2}.sil", StardustDir, "Bookmarks", dlg.InputText)

                Dim fi As New IO.FileInfo(fn)
                Dim [Continue] As Boolean = False

                If fi.Exists = True Then
                    Select Case MsgBox("If you continue creating this bookmark section another one will be lost." & _
                                       vbCrLf & "Do you want no continue creating this section?", MsgBoxStyle.YesNo)
                        Case MsgBoxResult.Yes
                            [Continue] = True
                        Case Else
                            [Continue] = False
                    End Select
                Else
                    [Continue] = True
                End If

                If [Continue] = True Then
                    Dim data = <?xml version="1.0" encoding="utf-8"?>
                               <ImageList Title=<%= dlg.InputText %>>

                               </ImageList>
                    Dim sw As New IO.StreamWriter(fn)
                    sw.Write(data.ToString)
                    sw.Close()

                    bSections.Add(dlg.InputText, fn)

                    RaiseEvent ListUpdated(bSections)

                    ListBox1.Items.Add(New Core.Controls.AlbumPreview With {
                                       .AlbumBase = dlg.InputText, .AlbumTitle = dlg.InputText,
                                       .Width = 175, .Height = 55
                                                }
                                   )
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Sub RemoveSection_Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            ListBox2.Items.Clear()

            Dim fn As String = bSections(ListBox1.SelectedItem.ToString)
            Dim fi As New IO.FileInfo(fn)
            fi.Delete()

            bSections.Remove(ListBox1.SelectedItem.ToString)
            ListBox1.Items.Remove(ListBox1.SelectedItem)

            RaiseEvent ListUpdated(bSections)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub RemoveImage_Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim ToRemove As New List(Of Object)

        For Each item As ImageControl In ListBox2.Items
            If item.Selected = True Then
                ToRemove.Add(item)
            End If
        Next

        For Each item As ImageControl In ToRemove
            CurSection.Remove(item.Base)
            UpdateSection(CurSection)
            ListBox2.Items.Remove(item)
        Next
    End Sub

End Class
